﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Collections;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class GoodsLog
    {
        public int Add(Chain.Model.GoodsLog model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into GoodsLog(");
            builder.Append("GoodsAccount,Type,GoodsID,TotalPrice,GoodsNumber,Remark,CreateTime,ShopID,UserID,ChangeShopID)");
            builder.Append(" values (");
            builder.Append("@GoodsAccount,@Type,@GoodsID,@TotalPrice,@GoodsNumber,@Remark,@CreateTime,@ShopID,@UserID,@ChangeShopID)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GoodsAccount", SqlDbType.VarChar, 50), new SqlParameter("@Type", SqlDbType.Int, 4), new SqlParameter("@GoodsID", SqlDbType.Int, 4), new SqlParameter("@TotalPrice", SqlDbType.Money, 8), new SqlParameter("@GoodsNumber", SqlDbType.Int, 4), new SqlParameter("@Remark", SqlDbType.VarChar, 500), new SqlParameter("@CreateTime", SqlDbType.DateTime), new SqlParameter("@ShopID", SqlDbType.Int, 4), new SqlParameter("@UserID", SqlDbType.Int, 4), new SqlParameter("@ChangeShopID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.GoodsAccount;
            cmdParms[1].Value = model.Type;
            cmdParms[2].Value = model.GoodsID;
            cmdParms[3].Value = model.TotalPrice;
            cmdParms[4].Value = model.GoodsNumber;
            cmdParms[5].Value = model.Remark;
            cmdParms[6].Value = model.CreateTime;
            cmdParms[7].Value = model.ShopID;
            cmdParms[8].Value = model.UserID;
            cmdParms[9].Value = model.ChangeShopID;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int ID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from GoodsLog ");
            builder.Append(" where ID=@ID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string IDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from GoodsLog ");
            builder.Append(" where ID in (" + IDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool ExeclDataInput(ArrayList sqlArray)
        {
            return DbHelperSQL.ExecuteSqlTran(sqlArray);
        }

        public bool Exists(int ID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from GoodsLog");
            builder.Append(" where ID=@ID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public int GetIDByGoodsAccount(string GoodsAccount)
        {
            DataSet set = DbHelperSQL.Query(string.Format("select ID from  GoodsLog  where GoodsAccount='{0}'", GoodsAccount));
            if (set.Tables[0].Rows.Count > 0)
            {
                return Convert.ToInt32(set.Tables[0].Rows[0][0]);
            }
            return 0;
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ID,GoodsAccount,Type,GoodsID,TotalPrice,GoodsNumber,Remark,CreateTime,ShopID,UserID,ChangeShopID ");
            builder.Append(" FROM GoodsLog ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" ID,GoodsAccount,Type,GoodsID,TotalPrice,GoodsNumber,Remark,CreateTime,ShopID,UserID,ChangeShopID ");
            builder.Append(" FROM GoodsLog ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.ID desc");
            }
            builder.Append(")AS Row, T.*  from GoodsLog T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListSP(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "GoodsLog,SysShop,SysUser";
            string[] column = new string[] { "GoodsLog.*,SysShop.ShopName,SysUser.UserName,(select ShopName from SysShop where GoodsLog.ChangeShopID=SysShop.ShopID) as ChangeShopName" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "CreateTime", "ID", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public DataSet GetListsss(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ID,GoodsAccount,Type,GoodsID,TotalPrice,GoodsNumber,Remark,CreateTime,GoodsLog.ShopID,UserID,ChangeShopID,ShopName as ChangeShopName ");
            builder.Append(" FROM GoodsLog join SysShop on GoodsLog.ChangeShopID=SysShop.ShopID  ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("ID", "GoodsLog");
        }

        public Chain.Model.GoodsLog GetModel(int ID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 ID,GoodsAccount,Type,GoodsID,TotalPrice,GoodsNumber,Remark,CreateTime,ShopID,UserID,ChangeShopID from GoodsLog ");
            builder.Append(" where ID=@ID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ID;
            Chain.Model.GoodsLog log = new Chain.Model.GoodsLog();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["ID"] != null) && (set.Tables[0].Rows[0]["ID"].ToString() != ""))
            {
                log.ID = int.Parse(set.Tables[0].Rows[0]["ID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["GoodsAccount"] != null) && (set.Tables[0].Rows[0]["GoodsAccount"].ToString() != ""))
            {
                log.GoodsAccount = set.Tables[0].Rows[0]["GoodsAccount"].ToString();
            }
            if ((set.Tables[0].Rows[0]["Type"] != null) && (set.Tables[0].Rows[0]["Type"].ToString() != ""))
            {
                log.Type = int.Parse(set.Tables[0].Rows[0]["Type"].ToString());
            }
            if ((set.Tables[0].Rows[0]["GoodsID"] != null) && (set.Tables[0].Rows[0]["GoodsID"].ToString() != ""))
            {
                log.GoodsID = int.Parse(set.Tables[0].Rows[0]["GoodsID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["TotalPrice"] != null) && (set.Tables[0].Rows[0]["TotalPrice"].ToString() != ""))
            {
                log.TotalPrice = decimal.Parse(set.Tables[0].Rows[0]["TotalPrice"].ToString());
            }
            if ((set.Tables[0].Rows[0]["GoodsNumber"] != null) && (set.Tables[0].Rows[0]["GoodsNumber"].ToString() != ""))
            {
                log.GoodsNumber = int.Parse(set.Tables[0].Rows[0]["GoodsNumber"].ToString());
            }
            if ((set.Tables[0].Rows[0]["Remark"] != null) && (set.Tables[0].Rows[0]["Remark"].ToString() != ""))
            {
                log.Remark = set.Tables[0].Rows[0]["Remark"].ToString();
            }
            if ((set.Tables[0].Rows[0]["CreateTime"] != null) && (set.Tables[0].Rows[0]["CreateTime"].ToString() != ""))
            {
                log.CreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CreateTime"].ToString());
            }
            if ((set.Tables[0].Rows[0]["ShopID"] != null) && (set.Tables[0].Rows[0]["ShopID"].ToString() != ""))
            {
                log.ShopID = int.Parse(set.Tables[0].Rows[0]["ShopID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["UserID"] != null) && (set.Tables[0].Rows[0]["UserID"].ToString() != ""))
            {
                log.UserID = int.Parse(set.Tables[0].Rows[0]["UserID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["ChangeShopID"] != null) && (set.Tables[0].Rows[0]["ChangeShopID"].ToString() != ""))
            {
                log.ChangeShopID = int.Parse(set.Tables[0].Rows[0]["ChangeShopID"].ToString());
            }
            return log;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM GoodsLog ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(Chain.Model.GoodsLog model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update GoodsLog set ");
            builder.Append("GoodsAccount=@GoodsAccount,");
            builder.Append("Type=@Type,");
            builder.Append("GoodsID=@GoodsID,");
            builder.Append("TotalPrice=@TotalPrice,");
            builder.Append("GoodsNumber=@GoodsNumber,");
            builder.Append("Remark=@Remark,");
            builder.Append("CreateTime=@CreateTime,");
            builder.Append("ShopID=@ShopID,");
            builder.Append("UserID=@UserID,");
            builder.Append("ChangeShopID=@ChangeShopID");
            builder.Append(" where ID=@ID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GoodsAccount", SqlDbType.VarChar, 50), new SqlParameter("@Type", SqlDbType.Int, 4), new SqlParameter("@GoodsID", SqlDbType.Int, 4), new SqlParameter("@TotalPrice", SqlDbType.Money, 8), new SqlParameter("@GoodsNumber", SqlDbType.Int, 4), new SqlParameter("@Remark", SqlDbType.VarChar, 500), new SqlParameter("@CreateTime", SqlDbType.DateTime), new SqlParameter("@ShopID", SqlDbType.Int, 4), new SqlParameter("@UserID", SqlDbType.Int, 4), new SqlParameter("@ChangeShopID", SqlDbType.Int, 4), new SqlParameter("@ID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.GoodsAccount;
            cmdParms[1].Value = model.Type;
            cmdParms[2].Value = model.GoodsID;
            cmdParms[3].Value = model.TotalPrice;
            cmdParms[4].Value = model.GoodsNumber;
            cmdParms[5].Value = model.Remark;
            cmdParms[6].Value = model.CreateTime;
            cmdParms[7].Value = model.ShopID;
            cmdParms[8].Value = model.UserID;
            cmdParms[9].Value = model.ChangeShopID;
            cmdParms[10].Value = model.ID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

